﻿package com.PlantVsZombies.Plants;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;

import javax.swing.ImageIcon;

import com.PlantVsZombies.Bullet.Bullet;
import com.PlantVsZombies.common.Plant;
import com.PlantVsZombies.constant.PlantType;
import com.PlantVsZombies.util.ImageUtil;
import com.PlantVsZombies.util.MusicPlay;

/**
 * 
 * 模式二植物
 * 
 * @author Administrator
 *
 */
public class FightPlant extends Plant {
	private int x;
	private int y;
	private int life;
	private int imageIndex = 0; // 图片标记
	private int bulletspeed = 30; // 发射子弹速度
	private int shootIndex = 0; // 发射子弹标记
	private int fire = 1;
	private int energy;
	private int bigbullet = 1;
	private boolean isWudi = false;
	MusicPlay bulletmusic = new MusicPlay("music/bullet.wav");

	ArrayList<Bullet> bullet = new ArrayList<Bullet>();

	public ArrayList<Bullet> getBullet() {
		return bullet;
	}

	public void setBullet(ArrayList<Bullet> bullet) {
		this.bullet = bullet;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getLife() {
		return life;
	}

	public void setLife(int life) {
		this.life = life;
	}

	public int getFire() {
		return fire;
	}

	public void setFire(int fire) {
		this.fire = fire;
	}

	public int getBigbullet() {
		return bigbullet;
	}

	public void setBigbullet(int bigbullet) {
		this.bigbullet = bigbullet;
	}

	public int getEnergy() {
		return energy;
	}

	public void setEnergy(int energy) {
		this.energy = energy;
	}

	public FightPlant(int x, int y) {
		this.x = x;
		this.y = y;
		this.plantImage = new ImageIcon("images/pattern2/player1.png");
		this.canShoot = true;
		this.life = 10;
	}

	public boolean isWudi() {
		return isWudi;
	}

	public void setWudi(boolean isWudi) {
		this.isWudi = isWudi;
	}

	@Override
	public void drawMe(Graphics g, int x, int y) {
		// TODO Auto-generated method stub
		this.x = x;
		this.y = y;
		drawBullet(g);
		// g.drawImage(plantImage.getImage(), x, y, null);
		if (isWudi)
			g.drawImage(ImageUtil.playerwudi, x, y,60,80, null);
		else
			g.drawImage(plantImage.getImage(), x, y, 60, 80, null);
		drawLife(g);
		drawEnegy(g);
	}

	public void drawLife(Graphics g) {
		g.drawImage(ImageUtil.PEAPLANT0.getImage(), 5, 10, 80, 50, null);
		g.drawImage(new ImageIcon("images/pattern2/life1.PNG").getImage(), 100,27,154,20,null);
		g.setColor(Color.GREEN);
		g.fillRect(102, 30, 150 * life / 10, 14);
	}

	public void drawEnegy(Graphics g) {
		g.setColor(Color.GRAY);
		g.drawRect(118, 550, 204, 20);
		g.setColor(Color.YELLOW);
		g.fillRect(120, 552, 200 * energy / 10, 15);
		g.drawString("能量", 30, 570);
		g.drawString("X"+bigbullet, 340, 570);
	}

	// 画子弹
	public void drawBullet(Graphics g) {
		for (int i = 0; i < bullet.size(); i++) {
			Bullet bt = bullet.get(i);
			if (bt.isLive()) {
				bt.drawMe(g);
			} else
				bullet.remove(bt);
		}
	}

	public void moveUp() {
		if (this.y > 0)
			this.y -= 20;
	}

	public void moveRight() {
		if (this.x < 1000)
			this.x += 20;
	}

	public void moveLeft() {
		if (this.x > 0)
			this.x -= 20;
	}

	public void moveDown() {
		if (this.y < 520) {
			this.y += 20;
		}
	}

	@Override
	public void shootBullet() {
		// TODO Auto-generated method stub
		bulletmusic.replay();
		Bullet bt = new Bullet(x, y + 20, ImageUtil.playerbullet[fire - 1], 1);
		bt.start();
		bullet.add(bt);
	}

	public void shootBigBullet() {
		if (bigbullet > 0) {
			for (int j = 0; j < 5; j++)
				for (int i = 0; i < 20; i++) {
					Bullet bt = new Bullet(-100 + 100 * j, 10 + i * 50,
							ImageUtil.bigbullet.getImage(), 1);
					bt.start();
					bullet.add(bt);
				}
			bigbullet--;
		}

	}

	@Override
	public PlantType getType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int doRun() {
		// TODO Auto-generated method stub
		try {
			Thread.sleep(150);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 1;
	}

}
